package zero.tech.games.mj.util;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.ZeroContext;
import zero.tech.core.util.RandomUtil;
import zero.tech.games.common.BigCalSource;
import zero.tech.games.common.QueueConsts;
import zero.tech.games.common.playback.Playback;
import zero.tech.games.common.playback.PlaybackBasic;
import zero.tech.games.entity.games.Club;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.common.RoomCache;
import zero.tech.games.logic.data.res.ResSeat;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.games.mj.data.MjBigPanel;
import zero.tech.games.mj.data.MjGameBase;
import zero.tech.games.mj.data.MjSmallPanel;
import zero.tech.games.mj.data.entity.PlayerSmallCal;
import zero.tech.games.mj.data.playback.MjPlaybackBigOverStep;
import zero.tech.games.mj.data.playback.MjPlaybackFapaiStep;
import zero.tech.games.mj.data.playback.MjPlaybackOperateStep;
import zero.tech.games.mj.data.playback.MjPlaybackSmallOverStep;
import zero.tech.redis.queue.QueueUtil;
import zero.tech.wsnet.controller.LogicController;

/**
 * 战绩管理工具
 * @author ludry
 *
 */
public class PlaybackUtil {
	
	/**
	 * 保存回放数据
	 * @param room
	 * @param source
	 */
	public static void saveClubBigPlayback(Room room, int source) {
		Club club=room.getClub();
		if(club!=null) {
			RoomCache roomCache = room.getRoomCache();
			if(source==BigCalSource.SMALLOVER_BIG_CAL) {
				int club_id=club.getClub_id();
				int game_id=room.getGame_id();
				String room_id=room.getRoom_id();
				String config_str=room.getConfigStr();
				int room_times=room.getRoom_times();
				String play_code=RandomUtil.randNum(8);
				Playback playback=new Playback();
				playback.setCreate_stamp(new Date());
				playback.setGame_id(game_id);
				playback.setClub_id(club_id);
				playback.setRoom_id(room_id);
				playback.setRoom_times(room_times);
				playback.setPlay_code(play_code);
				playback.setConfig_str(config_str);
				playback.setFapai_step(packageFapaiStep(room));
				playback.setOperate_step(packageOperateStep(room));
				playback.setSmallover_step(packageSmallOverStep(room));
				playback.setBigover_step(packageBigOverStep(room));
				playback.getUids().addAll(room.getAllControllers().keySet());
				MjSmallPanel mjSmallPanel = (MjSmallPanel) roomCache.getSmallPanels().get(room_times);
				for(PlayerSmallCal playerSmallCal : mjSmallPanel.getRoles().values()) {
					playback.getBasics().add(packagePlaybackBasic(playerSmallCal));
				}
				roomCache.getPlaybacks().add(playback);
			}
			QueueUtil queueUtil=ZeroContext.getInstance(QueueUtil.class);
			queueUtil.putQueueBatch(QueueConsts.QUEUE_PLAYBACK,roomCache.getPlaybacks());
		}
	}
	
	/**
	 * 临时保存小结算
	 * @param room
	 */
	public static void saveClubSmallPlayback(Room room) {
		Club club=room.getClub();
		if(club!=null) {
			RoomCache roomCache = room.getRoomCache();
			int club_id=club.getClub_id();
			int game_id=room.getGame_id();
			String config_str=room.getConfigStr();
			String play_code=RandomUtil.randNum(8);
			String room_id=room.getRoom_id();
			int room_times=room.getRoom_times();
			Playback playback=new Playback();
			playback.setCreate_stamp(new Date());
			playback.setClub_id(club_id);
			playback.setGame_id(game_id);
			playback.setRoom_id(room_id);
			playback.setRoom_times(room_times);
			playback.setConfig_str(config_str);
			playback.setPlay_code(play_code);
			playback.setFapai_step(packageFapaiStep(room));
			playback.setOperate_step(packageOperateStep(room));
			playback.setSmallover_step(packageSmallOverStep(room));
			playback.getUids().addAll(room.getAllControllers().keySet());
			MjSmallPanel mjSmallPanel = (MjSmallPanel) roomCache.getSmallPanels().get(room_times);
			for(PlayerSmallCal playerSmallCal : mjSmallPanel.getRoles().values()) {
				playback.getBasics().add(packagePlaybackBasic(playerSmallCal));
			}
			roomCache.getPlaybacks().add(playback);
		}
	}
	
	/**
	 * 回放分数记录
	 * @param playerSmallCal
	 * @return
	 */
	private static PlaybackBasic packagePlaybackBasic(PlayerSmallCal playerSmallCal) {
		PlaybackBasic playbackBasic= new PlaybackBasic();
		playbackBasic.setSimpleUser(playerSmallCal.getSimpleUser());
		playbackBasic.setCur_win_gold(playerSmallCal.getCur_win_gold());
		playbackBasic.setCur_win_score(playerSmallCal.getCur_win_score());
		return playbackBasic;
	}
	
	/**
	 * 大结算战绩处理
	 * @param room
	 * @return
	 */
	private static JSONObject packageBigOverStep(Room room) {
		MjPlaybackBigOverStep playbackBigOverStep = new MjPlaybackBigOverStep();
		RoomCache roomCache = room.getRoomCache();
		MjBigPanel bigPanel=(MjBigPanel) roomCache.getBigPanel();
		playbackBigOverStep.setMjBigPanel(bigPanel);
		return (JSONObject) JSONObject.toJSON(playbackBigOverStep);
	}
	
	/**
	 * 小结算战绩处理
	 * @param room
	 * @return
	 */
	private static JSONObject packageSmallOverStep(Room room) {
		RoomCache roomCache = room.getRoomCache();
		int room_times=room.getRoom_times();
		MjPlaybackSmallOverStep smallOverStep = new MjPlaybackSmallOverStep();
		MjSmallPanel smallPanel = (MjSmallPanel) roomCache.getSmallPanels().get(room_times);
		smallOverStep.setMjSmallPanel(smallPanel);
		return (JSONObject) JSONObject.toJSON(smallOverStep);
	}

	/**
	 * 打牌战绩处理
	 * @param room
	 * @return
	 */
	private static JSONObject packageOperateStep(Room room) {
		MjGameBase gameBase = (MjGameBase) room.getGameBase();
		MjPlaybackOperateStep operateStep = new MjPlaybackOperateStep();
		//操作数据
		operateStep.setPlayDetail(gameBase.getPlaybackDetail().getPlayDetail());
		return (JSONObject) JSONObject.toJSON(operateStep);
	}

	/**
	 * 发牌战绩处理
	 * @param room
	 * @return
	 */
	private static JSONObject packageFapaiStep(Room room) {
		MjPlaybackFapaiStep fapaiStep = new MjPlaybackFapaiStep();
		MjGameBase pdkGameBase = (MjGameBase) room.getGameBase();
		//庄家id
		fapaiStep.sethUid(pdkGameBase.getHuid());
		//座位信息
		Map<Integer,Integer> map =new HashMap<Integer, Integer>();
		map.putAll(room.getSeats());
		for(Entry<Integer,Integer> entry : map.entrySet()) {
			Integer pos = entry.getKey();
			Integer uid = entry.getValue();
			if(uid != null) {
				LogicController logicController = room.getAllControllers().get(uid);
				ResSeat seat = PckUtil.packageResSeat(logicController);
				fapaiStep.getRes_seats().put(pos, seat);
			}
		}
		//手牌信息
		for (Entry<Integer,List<Integer>> entry : pdkGameBase.getPlaybackDetail().getHandsmap().entrySet()) {
			fapaiStep.getHandsmap().put(entry.getKey(), entry.getValue());
		}
		//鬼牌信息
		fapaiStep.setGuiCards(pdkGameBase.getPlaybackDetail().getGuiCards());
		return (JSONObject) JSONObject.toJSON(fapaiStep);
	}

}
